package com.wjx.user_provider.controller;

import com.wjx.user_provider.dao.AppointmentDao;
import com.wjx.user_provider.dao.RatingDao;
import com.wjx.user_provider.dao.ServeDao;
import com.wjx.user_provider.dao.UserDao;
import com.wjx.user_provider.model.Appointment;
import com.wjx.user_provider.model.Rating;
import com.wjx.user_provider.model.Serve;
import com.wjx.user_provider.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class UserController {
    @Autowired
    private UserDao userDao;
    @Autowired
    private ServeDao serveDao;
    @Autowired
    private RatingDao ratingDao;
    @Autowired
    private AppointmentDao appointmentDao;

    @PostMapping("/login")
    public User login(String username) {
        return userDao.selectUserByUsername(username);
    }

    @PostMapping("/register")
    public int register(String username, String password, String authority) {
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        user.setAuthority(authority);
        return userDao.addUser(user);

    }

    @PostMapping("/user/index")
    public String index(Model model) {
        model.addAttribute("choice", 0);
        return "user/index";
    }

    @PostMapping("/service-center")
    public List<Serve> service_center() {
        return serveDao.queryAllService();
    }

    @PostMapping("/search")
    public List<Serve> search(String keyword) {
        return serveDao.queryLikeService(keyword);
    }

    @PostMapping("/user-queryServiceById")
    public Serve queryServiceById(int id) {
        return serveDao.queryServiceById(id);
    }

    @PostMapping("/user-queryRatingByServiceId")
    public List<Rating> queryRatingByServiceId(int id) {
        return ratingDao.queryRatingByServiceId(id);
    }

    @PostMapping("/user-queryUserById")
    public User queryUserById(int id) {
        return userDao.queryUserById(id);
    }

    @PostMapping("/user-addAppointment")
    public int addAppointment(int userId, int serviceId, String appointmentTime,
                              String appointmentLocation, String phone, String status,
                              String createdTime, String lastUpdatedTime) {
        Appointment appointment = new Appointment(userId, serviceId, appointmentTime,
                appointmentLocation, phone, status, createdTime, lastUpdatedTime);
        return appointmentDao.addAppointment(appointment);
    }

    @PostMapping("/user-queryAppointmentByUserId")
    public List<Appointment> queryAppointmentByUserId(int id) {
        return appointmentDao.queryAppointmentByUserId(id);
    }

    @PostMapping("/user-updateScoreById")
    public int updateScoreById(int id, int score, String feedback, String lastUpdatedTime) {
        return ratingDao.updateScoreById(id, score, feedback, lastUpdatedTime);
    }

    @PostMapping("/user-queryRatingById")
    public Rating queryRatingById(int id) {
        return ratingDao.queryRatingById(id);
    }

    @PostMapping("/user-deleteRatingById")
    public int deleteRatingById(int id) {
        return ratingDao.deleteRatingById(id);
    }

    @PostMapping("/user-queryRatingByAppointmentId")
    public Rating queryRatingByAppointmentId(int appointmentId) {
        return ratingDao.queryRatingByAppointmentId(appointmentId);
    }

    @PostMapping("/user-addRating")
    public int addRating(int serviceId,int appointmentId,int userId,int score,
                         String feedback,String createdTime,String lastUpdatedTime) {
        Rating rating = new Rating(serviceId,userId,appointmentId,score,
                feedback,createdTime,lastUpdatedTime);
        return ratingDao.addRating(rating);
    }

    @PostMapping("/user-updateUserById")
    public int updateUserById( int id,String username,String password,String authority) {
        User user = new User(id,username,password,authority);
        return userDao.updateUserById(user);
    }
}
